//回复功能
function comment(targetId, type, content) {
    if (!content) {
        // alert("内容不得为空····~");
        sweetAlert("出错啦...", "不能回复空内容~~~", "error");
        return;
    }
    $.ajax({
            type: "POST",
            url: "/comment",
            contentType: "application/json",
            data: JSON.stringify({
                    "parentId": targetId,
                    "content": content,
                    "type": type
                }
            ),
            success: function (response) {
                if (response.code === 200) {
                    swal({
                        title: "回复成功！",
                        text: "点击确认",
                        icon: "success",
                        button: "确认",
                    }).then((value) => {
                        window.location.reload();
                    });
                    window.location.load();
                } else {
                    //登录操作
                    login(response);
                }
            }
            ,
            dataType: "json"
        }
    );
}

//登录操作
function login(response) {
    if (response.code === 2006) {
        swal({
            title: "错误：" + response.code,
            text: response.message,
            icon: "warning",
            buttons: true,
            dangerMode: true,
        }).then((willDelete) => {
            if (willDelete) {
                window.open("https://gitee.com/oauth/authorize?client_id=da0b07bbbcbfc5eb7bdc0996f77e92af8440c44c83d09055eaac1db5ce56262a&custom=tom" +
                    "&redirect_uri=http://localhost:8886/callback&response_type=code&state=1");
                window.localStorage.setItem("closable", true);
                // var isAccept = confirm(response.message);
                // if (isAccept) {
                //     window.open("https://gitee.com/oauth/authorize?client_id=da0b07bbbcbfc5eb7bdc0996f77e92af8440c44c83d09055eaac1db5ce56262a&custom=tom&redirect_uri=http://localhost:8886/callback&response_type=code&state=1");
                //     window.localStorage.setItem("closable", true);//用来判断是否需要关闭页面
                // }
            } else {
                swal({
                    title: "已取消登录!",
                    text: "取消登陆后，无法成功回复!",
                    icon: "error",
                    button: "确认",
                });
            }
        });
    } else {
        // alert(response.message);
        sweetAlert("错误：" + response.code, response.message, "error");
    }
}

/*问题评论*/
function post() {
    /*获取问题Id*/
    var questionId = $("#question_id").val();
    /*获取评论内容*/
    var content = $("#question_content").val();
    if (content.length > 1024)
        swal({
            title: "回复超过1024个字长!",
            text: "你的回复字数为:" + content.length + "，请精简您的发言!",
            icon: "warning",
            button: "确认",
        });
    else comment(questionId, 0, content);
}

/*二级评论*/
function subComment(e) {
    /*获取评论Id*/
    var commentId = e.getAttribute("data-id");
    /*获取评论内容*/
    var content = $("#input-" + commentId).val();
    if (content.length > 1024)
        swal({
            title: "回复超过1024个字长!",
            text: "你的回复字数为:" + content.length + "，请精简您的发言!",
            icon: "warning",
            button: "确认",
        });
    else comment(commentId, 1, content);
}

//二级评论的展开
function collapseComment(e) {
    //评论id
    var id = e.getAttribute("data-id");
    //定位二级评论位置
    var subCommentContainer = $("#comment-" + id);

    //获取二次评论的展开状态
    var collapse = e.getAttribute("collapse-data");
    if (collapse) {
        subCommentContainer.removeClass("in");
        e.removeAttribute("collapse-data");
        e.classList.remove("active");
    } else {
        //添加in类，使得二次评论展开
        subCommentContainer.addClass("in");
        //用于判断
        e.setAttribute("collapse-data", "in");
        //用于添加持续运行的属性
        e.classList.add("active");

        if (subCommentContainer.children().length != 1) {
            //添加in类，使得二次评论展开
            subCommentContainer.addClass("in");
            //用于判断
            e.setAttribute("collapse-data", "in");
            //用于添加持续运行的属性
            e.classList.add("active");
        } else {
            //添加in类，使得二次评论展开
            subCommentContainer.addClass("in");
            //用于判断
            e.setAttribute("collapse-data", "in");
            //用于添加持续运行的属性
            e.classList.add("active");
            //二级评论列表
            $.getJSON("/comment/" + id, function (data) {
                $.each(data.data.reverse(), function (index, comment) {
                    //头像名字
                    var mediaLeftElement = $("<div/>", {
                        "class": "media-left"
                    }).append($("<img/>", {
                        "class": "media-object img-rounded",
                        "src": comment.user.avatarUrl
                    }));
                    //评论内容
                    var mediaBodyElement = $("<div/>", {
                        "class": "media-body"
                    }).append($("<h6/>", {
                        "class": "media-heading comment-heading",
                        "html": comment.user.name
                    })).append($("<div/>", {
                        "html": comment.content
                    })).append($("<div/>", {
                        "class": "menu"
                    }).append($("<div/>", {
                        "class": "pull-right",
                        "html": moment(comment.gmtCreate).format('YYYY-MM-DD')
                    })));

                    var mediaElement = $("<div/>", {
                        "class": "media"
                    }).append(mediaLeftElement).append(mediaBodyElement);

                    var commentElement = $("<div/>", {
                        "class": "col-lg-12 col-md-12 col-sm-12 col-xs-12 comments"
                    }).append(mediaElement);

                    subCommentContainer.prepend(commentElement);
                })
            })
        }
    }
}

//标签的展开
function showSelectTag() {
    $("#select-tag").show();
}

//选取标签内容
function selectTag(e) {
    let tag = e.getAttribute("data-tag");
    var previous = $("#tag").val();
    if (previous.indexOf(tag) === -1) {
        if (previous) {
            $("#tag").val(previous + "," + tag);
        } else {
            $("#tag").val(tag);
        }
    }
}

//评论点赞
function likeComment(e) {
    //评论id
    var commentId = e.getAttribute("data-id");
    like(commentId, 1, 1);
}

//收藏问题
function likeQuestion(e) {
    //评论id
    var questionId = e.getAttribute("data-id");
    like(questionId, 0, 1);
}

//点赞
function like(targetId, type, status) {
    $.ajax({
        type: "POST",
        url: "/thumb",
        contentType: "application/json",
        data: JSON.stringify({
                "targetId": targetId,
                "status": status,
                "type": type
            }
        ),
        success: function (response) {
            if (response.code === 200) {
                if (type === 1) {//点赞评论时
                    var thumb_icon = $("#thumb-icon-" + targetId);
                    thumb_icon.add("active");
                    var thumb_count = $("#thumb-count-" + targetId);
                    thumb_count.html(parseInt(thumb_count.text()) + 1);//点赞+1
                    window.location.reload();
                }
                if (type === 0) {//收藏问题时
                    var thumbicon = $("#collect-question-" + targetId);
                    thumbicon.removeClass("glyphicon-heart-empty");
                    thumbicon.addClass("glyphicon-heart");
                    var likecount = $("#collect-count-" + targetId);
                    likecount.html(parseInt(likecount.text()) + 1);//点赞+1
                }
            } else if (response.code === 2012) {
                if (type === 1) {//点赞评论时
                    swal({
                        title: "点赞失败!",
                        text: "请不要重复点赞哦!",
                        icon: "error",
                        button: "确认",
                    });
                } else if (type === 0) {//点赞评论时
                    swal({
                        title: "收藏失败!",
                        text: "请不要重复收藏哦!",
                        icon: "error",
                        button: "确认",
                    });
                }

            } else if (response.code === 2006) {
                //登录操作
                login(response);
            }
        }
        ,
        dataType: "json"
    })
}